package com.xinghou.platform.file.handler.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.chain.impl.ContextBase;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.xinghou.platform.file.chain.impl.MsgVoiceUploadChain;
import com.xinghou.platform.file.chain.impl.ShopPhotoUploadChain;
import com.xinghou.platform.file.handler.BaseHandler;
import com.xinghou.platform.file.model.ResourceContext;
import com.xinghou.platform.util.ConfigUtil;
import com.xinghou.platform.util.DateUtils;
import com.xinghou.platform.util.JsonUtil;

@Service
public class UpShopPhotoHandler implements BaseHandler {
	@Resource
	private ShopPhotoUploadChain shopPhotoUploadChain;
	
	@Override
	public void execute(ContextBase context) throws Exception {
		@SuppressWarnings("unchecked")
		ResourceContext<byte[], String> resourceContext = (ResourceContext<byte[], String>)context;
		resourceContext.getResponse().setResultCode(0);
		//TODO
		try{
			shopPhotoUploadChain.execute(resourceContext);
		}catch(Throwable e)
		{
			e.printStackTrace();
		}
		if(resourceContext.getResponse().getResultCode() == 0){
			Map<String, String> result = new HashMap<String, String>();
			result.put("result", "0");
			result.put("ckresult", "0");
			//System.out.println(context.get("pid"));
			result.put("imgid",  String.valueOf(Integer.valueOf(context.get("pid").toString())));
			result.put("imgurl", ConfigUtil.getProperty("shopimg_domain")+resourceContext.getSource().getRootPath() + resourceContext.getSource().getPath()
					+ resourceContext.getSource().getSubPath().replace("\\", "/")+"/"+resourceContext.getSource().getResourceName());
			result.put("createTime", DateUtils.getNewZhCNDateTime());
			result.put("errcode", "0");
			result.put("errmsg", "success");
			resourceContext.getResponse().setBody(JsonUtil.toJson(result));
			resourceContext.getResponse().getResponseHeaders().set("ContentType", "application/json;charset=UTF-8");
			resourceContext.getResponse().setStatusCode(HttpStatus.OK);
		}else if(resourceContext.getResponse().getResultCode() > 0){
			resourceContext.getResponse().getResponseHeaders().set("ContentType", "application/json;charset=UTF-8");
			resourceContext.getResponse().setStatusCode(HttpStatus.OK);
		}else{
			resourceContext.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
		}
		
	}

}
